package com.bwie.doctorclient.service.impl;

import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.doctorclient.domain.dto.AddCommonlyDosageDTO;
import com.bwie.doctorclient.domain.dto.AddCommonlyPrescriptionDTO;
import com.bwie.doctorclient.domain.po.CommonlyDosagePO;
import com.bwie.doctorclient.domain.po.CommonlyPrescriptionPO;
import com.bwie.doctorclient.domain.vo.CommonlyDosageVO;
import com.bwie.doctorclient.domain.vo.CommonlyPrescriptionVO;
import com.bwie.doctorclient.mapper.PrescriptionMapper;
import com.bwie.doctorclient.service.PrescriptionService;
import com.bwie.system.api.model.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    private PrescriptionMapper prescriptionMapper;

    /**
     * 获取常用处方列表
     * @return
     */
    @Override
    public List<CommonlyPrescriptionVO> getCommonlyPrescription() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据医生id查询常用处方
        List<CommonlyPrescriptionVO> commonlyPrescriptionVOList = prescriptionMapper.getCommonlyPrescriptionByOrderId(loginUser.getUserid());

        for (CommonlyPrescriptionVO commonlyPrescriptionVO : commonlyPrescriptionVOList) {
            //查询常用处方下的常用药品用法用量
             List<CommonlyDosageVO> commonlyDosageVOS = prescriptionMapper.getDosageByPrescriptionId(commonlyPrescriptionVO.getId());
             commonlyPrescriptionVO.setCommonlyDosageVOS(commonlyDosageVOS);
        }
        return commonlyPrescriptionVOList;
    }

    /**
     * 删除常用处方
     * @param commonlyPrescriptionId
     * @return
     */
    @Transactional
    @Override
    public void delCommonlyPrescription(Long commonlyPrescriptionId) {
        //根据常用处方id删除常用处方的药品用法用量信息
        prescriptionMapper.delDosageByPrescriptionId(commonlyPrescriptionId);
        //根据常用处方id删除常用处方药品用法用量中间表
        prescriptionMapper.delDosageRelationByPrescriptionId(commonlyPrescriptionId);
        //根据常用处方id删除常用处方
        prescriptionMapper.delCommonlyPrescription(commonlyPrescriptionId);
    }

    /**
     * 新增常用处方
     * @return
     */
    @Transactional
    @Override
    public void addCommonlyPrescription(AddCommonlyPrescriptionDTO addCommonlyPrescriptionDTO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CommonlyPrescriptionPO commonlyPrescriptionPO = new CommonlyPrescriptionPO();
        commonlyPrescriptionPO.setDoctorId(loginUser.getUserid());
        commonlyPrescriptionPO.setDiagnosis(addCommonlyPrescriptionDTO.getDiagnosis());
        //新增常用处方
        prescriptionMapper.addCommonlyPrescription(commonlyPrescriptionPO);

        for (CommonlyDosagePO commonlyDosagePO : addCommonlyPrescriptionDTO.getCommonlyDosagePOS()) {
            //新增常用处方药品用法用量
            prescriptionMapper.addDosage(commonlyDosagePO);
            //新增常用处方药品用法用量中间表
            prescriptionMapper.addDosageRelation(commonlyPrescriptionPO.getId(),commonlyDosagePO.getId());
        }
    }

    /**
     * 删除常用处方中药品用法用量
     * @param commonlyDosageId
     * @return
     */
    @Transactional
    @Override
    public void delCommonlyDosage(Long commonlyDosageId) {
        //根据id删除药品用法用量信息
        prescriptionMapper.delCommonlyDosage(commonlyDosageId);
        //根据药品用法用量id删除常用处方药品用法用量中间表
        prescriptionMapper.delDosageRelationByDosageId(commonlyDosageId);
    }

    /**
     * 编辑常用药品用法用量
     * @param commonlyDosagePO
     * @return
     */
    @Override
    public void updCommonlyDosage(CommonlyDosagePO commonlyDosagePO) {
        prescriptionMapper.updCommonlyDosage(commonlyDosagePO);
    }

    /**
     * 新增常用处方药品
     * @param addCommonlyDosageDTO
     * @return
     */
    @Transactional
    @Override
    public void addCommonlyDosage(AddCommonlyDosageDTO addCommonlyDosageDTO) {
        CommonlyDosagePO commonlyDosagePO = new CommonlyDosagePO();
        BeanUtils.copyProperties(addCommonlyDosageDTO,commonlyDosagePO);
        prescriptionMapper.addDosage(commonlyDosagePO);
        //新增常用处方药品用法用量中间表
        prescriptionMapper.addDosageRelation(addCommonlyDosageDTO.getCommonlyPrescriptionId(), commonlyDosagePO.getId());
    }

    /**
     * 编辑常用处方
     * @param commonlyPrescriptionPO
     * @return
     */
    @Override
    public void updateCommonlyPrescription(CommonlyPrescriptionPO commonlyPrescriptionPO) {
        prescriptionMapper.updateCommonlyPrescription(commonlyPrescriptionPO);
    }
}
